package com.grouponmall.service;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.grouponmall.model.Deal;

/**
 * 从HomeRun拿到的数据中，会有一部分城市没有今天的商品，这样的城市会直接跳过，后面可以考虑改进为记录相应的日志
 * 
 * @author xueqiang.mi
 * @since 2010-7-1
 * @modified 2010-10-10
 */
public class RetrieveFromHomeRun extends BaseRetrieveService {

  public static final String URL = "http://homerun.com/";
  public static final String SOURCE = "HomeRun";

  @Override
  public List<Deal> extract(String link) throws ParserException {
    List<Deal> deals = new ArrayList<Deal>();
    final Deal deal = new Deal();
    deal.setSource(SOURCE);
    deal.setLink(link);

    Parser parser = new Parser(link);
    NodeList nodeList = parser.extractAllNodesThatMatch(new OrFilter(new AndFilter(new LinkStringFilter("/"),
      new HasAttributeFilter("class", "selected-city")), new AndFilter(new TagNameFilter("div"),
      new HasAttributeFilter("class", "content"))));
    if (nodeList.size() == 3) {
      deal.setCity(nodeList.elementAt(0).getFirstChild().getText().trim());
      nodeList = nodeList.elementAt(2).getChildren();
    }

    // 开始提取数据
    NodeList temp = nodeList.extractAllNodesThatMatch(new HasAttributeFilter("class", "title rockwell"));
    if (temp.size() != 0) {
      deal.setTitle(temp.elementAt(0).getFirstChild().getText().trim());
    }

    temp = nodeList.extractAllNodesThatMatch(new HasAttributeFilter("class", "avalanche-bar-originally"), true);
    if (temp.size() != 0) {
      Node n = temp.elementAt(0);
      deal.setPrice(n.getChildren().elementAt(2).getText().trim());
    }

    temp = nodeList.extractAllNodesThatMatch(new HasAttributeFilter("class", "econ rockwell"), true);
    if (temp.size() == 3) {
      Node n = temp.elementAt(0);
      deal.setRetailValue(n.getFirstChild().getText().trim());
      n = temp.elementAt(2);
      deal.setDiscount(n.getFirstChild().getText().trim());
    }

    temp = nodeList.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("img"), new HasAttributeFilter("class",
      "deal")), true);
    if (temp.size() == 1) {
      ImageTag image = (ImageTag) temp.elementAt(0);
      deal.setImage(image.getAttribute("src"));
    }

    deals.add(deal);
    return deals;
  }

  @Override
  public NodeList getExpectedLinks(String link) throws ParserException {
    NodeList list = new NodeList();
    Node[] cityNodes = getCityLinks(link); // 获取所有城市的链接
    for (Node node : cityNodes) {
      LinkTag lt = (LinkTag) node;
      getDealLinks(list, lt.getLink());
    }
    return list;
  }

  private Node[] getCityLinks(String link) throws ParserException {
    Parser parser = new Parser(link);
    NodeList nodeList = parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("div"), new HasAttributeFilter(
      "class", "vertical-list")));
    nodeList = nodeList.extractAllNodesThatMatch(new LinkStringFilter("/"), true);
    decodingLink(nodeList);
    return nodeList.toNodeArray();
  }

  private void getDealLinks(NodeList list, String link) throws ParserException {
    Parser parser = new Parser(link);
    NodeList nodeList = parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("div"), new HasAttributeFilter(
      "class", "deal daily-deal")));
    if (nodeList.size() == 0) {
      return;
    }
    for (Node node : nodeList.toNodeArray()) {
      nodeList = node.getChildren().extractAllNodesThatMatch(
        new AndFilter(new LinkStringFilter("/"), new HasAttributeFilter("class", "deal-counter-link")), true);
      decodingLink(nodeList);
      list.add(nodeList);
    }

  }

  public String getURL() {
    return URL;
  }

  private void decodingLink(NodeList nodeList) {
    for (Node node : nodeList.toNodeArray()) {
      LinkTag lt = (LinkTag) node;
      String lnk = lt.getLink().replace("/&#47;", "/").replace("&#47;", "/");
      lt.setLink(lnk);
    }
  }

}
